package com.atguigu.webadmin.controller.system;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.SystemUser;
import com.atguigu.model.enums.BaseStatus;
import com.atguigu.webadmin.service.SystemUserService;
import com.atguigu.webadmin.vo.system.user.SystemUserItemVo;
import com.atguigu.webadmin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
@CrossOrigin
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "size",description = "需要展示的数量",in = ParameterIn.QUERY),
            @Parameter(name = "queryVo",description = "需要返回的vo对象",in = ParameterIn.DEFAULT)
    })
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> pageSystemUserByQuery(@RequestParam long current,
                                                @RequestParam long size, SystemUserQueryVo queryVo) {
        //封装查询条件
        IPage<SystemUserItemVo> iPage = new Page<>(current,size);
        //传入查询条件和需要返回的vo对象执行查询
        IPage<SystemUserItemVo> list = systemUserService.pageSystemUserByQuery(iPage,queryVo);
        //
        return Result.ok(list);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @Parameters({
            @Parameter(name = "id",description = "用户id",in = ParameterIn.QUERY)
    })
    @GetMapping("getById")
    public Result<SystemUserItemVo> getSystemUserById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = systemUserService.getSystemUserById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @Parameters({
            @Parameter(name = "systemUser",description = "后台用户对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdateSystemUser(@RequestBody SystemUser systemUser) {
        Boolean b = systemUserService.saveOrUpdateSystemUser(systemUser);
        return Result.ok(b);
    }

    @Operation(summary = "判断后台用户名是否可用")
    @Parameters({
            @Parameter(name = "username",description = "用户名",in = ParameterIn.DEFAULT)
    })
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUserNameAvailable(@RequestParam String username) {
        //创建查询包装类对象
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        //指定查询条件
        queryWrapper.eq(username != null,SystemUser::getUsername,username);
        //执行查询
        long count = systemUserService.count(queryWrapper);
        return Result.ok(count == 0);
    }

    @DeleteMapping("deleteById")
    @Parameters({
            @Parameter(name = "id",description = "用户id",in = ParameterIn.QUERY)
    })
    @Operation(summary = "根据ID删除后台用户信息")
    public Result<Boolean> removeById(@RequestParam Long id) {
        boolean b = systemUserService.removeById(id);
        return Result.ok(b);
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @Parameters({
            @Parameter(name = "id",description = "用户id",in = ParameterIn.QUERY),
            @Parameter(name = "status",description = "用户状态",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateStatusByUserId")
    public Result<Boolean> updateStatusByUserId(@RequestParam Long id,
                                       @RequestParam BaseStatus status) {
        //创建Lambda更新包装类
        LambdaUpdateWrapper<SystemUser> updateWrapper = new LambdaUpdateWrapper<>();
        //指定查询条件
        updateWrapper.eq(SystemUser::getId,id);
        //设置更改属性
        updateWrapper.set(SystemUser::getStatus,status);
        boolean update = systemUserService.update(updateWrapper);
        return Result.ok(update);
    }
}
